﻿namespace rtgen_guiclient
{
    using System;
    using System.Management;
    using System.Runtime.InteropServices;

    internal class MSIInstall
    {
        private bool IsConnected;
        private ManagementScope m_WorkingNamespace;

        public void ConnectLocalComputer()
        {
            this.m_WorkingNamespace = new ManagementScope();
            this.m_WorkingNamespace.Path = new ManagementPath(@"\\.\root\cimv2");
            try
            {
                this.m_WorkingNamespace.Connect();
                this.IsConnected = true;
            }
            catch (COMException exception)
            {
                this.m_WorkingNamespace = null;
                throw new ArgumentException("Server does not exists or access denied. \nConfigure firewall options.", exception);
            }
            catch (UnauthorizedAccessException exception2)
            {
                this.m_WorkingNamespace = null;
                throw new ArgumentException("Access denied or timeout expired. \nCheck if you are in domain Administrators group.", exception2);
            }
        }

        public void ConnectRemoteComputer(string computerName, string domainName, string userName, string password, TimeSpan connectionTimeout)
        {
            ConnectionOptions options = new ConnectionOptions();
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.Authentication = AuthenticationLevel.Default;
            if ((domainName == null) || (domainName == string.Empty))
            {
                options.Username = computerName + @"\" + userName;
                options.Authority = null;
            }
            else
            {
                options.Username = userName;
                options.Authority = "NTLMdomain:" + domainName;
            }
            options.Password = password;
            options.Timeout = connectionTimeout;
            options.EnablePrivileges = true;
            this.m_WorkingNamespace = new ManagementScope();
            this.m_WorkingNamespace.Path = new ManagementPath(@"\\" + computerName + @"\root\cimv2");
            this.m_WorkingNamespace.Options = options;
            try
            {
                this.m_WorkingNamespace.Connect();
                this.IsConnected = true;
            }
            catch (COMException exception)
            {
                this.m_WorkingNamespace = null;
                throw new ArgumentException("Server does not exists or access denied. \nCheck host name and configure firewall options.", exception);
            }
            catch (UnauthorizedAccessException exception2)
            {
                this.m_WorkingNamespace = null;
                throw new ArgumentException("Access denied or timeout expired. \nCheck if username, password and domain are correct and if user is a member of domain Administrators group.", exception2);
            }
        }

        public void InstallProduct(string msiFilePath, string installOptions, bool allUsers)
        {
            if (!this.IsConnected)
            {
                throw new InvalidOperationException("Object is not bound to WMI namespace. Use one of Connect methods before this operation.");
            }
            ManagementClass class2 = new ManagementClass(this.m_WorkingNamespace, new ManagementPath("Win32_Product"), new ObjectGetOptions());
            try
            {
                object[] args = new object[] { msiFilePath, installOptions, allUsers };
                uint num = (uint) class2.InvokeMethod("Install", args);
                if (num > 0)
                {
                    throw new Exception("Installation failed. Error code = " + num);
                }
            }
            catch (ManagementException exception)
            {
                throw new Exception("Installation failed. RPC Server Fault Error.: " + exception.Message, exception);
            }
        }
    }
}

